import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/index'
import Register from '../views/register'
import Login from '../views/login'
import NotFound from '../views/404'
import Home from '../views/Home'
import InfoShow from '../views/infoShow'
import FundList from '../views/FundList'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/index'
  },
  {
    path: '/index',
    name: 'Index',
    component: Index,
    children:[
      {
        path:'',
        component:Home
      },
      {
        path:'/home',
        name:'home',
        component:Home
      },
      {
        path:'/infoshow',
        name:'infoshow',
        component:InfoShow
      },
      {
        path:'/fundlist',
        name:'FundList',
        component:FundList
      }
    ]
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '*',
    name: 'Notfound',
    component: NotFound
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to,from,next) => {
  // 判断 localStorage 中是否有 token
  const isLogin = localStorage.Token ? true : false 
  if(to.path == '/register' || to.path == '/login'){
    next()
  }else{
    // token 存在才能进入初始化页面
    isLogin ? next() : next('/login')
  }
})

export default router
